/*
 * @Author: jianfanfan 1217572833@qq.com
 * @Date: 2025-04-01 11:40:00
 * @LastEditors: jianfanfan 1217572833@qq.com
 * @LastEditTime: 2025-04-01 19:21:43
 * @FilePath: \guozaoke\src\common\cache.module.ts
 * @Description: 缓存模块配置
 */
import { Module, Global } from '@nestjs/common';
import { CacheModule as NestCacheModule } from '@nestjs/cache-manager';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { CacheService } from './services/cache.service';
import redisConfig from '../config/redis.config';

@Global()
@Module({
  imports: [
    ConfigModule.forFeature(redisConfig),
    NestCacheModule.registerAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        const redisOptions = configService.get('redis');
        return {
          ttl: redisOptions.ttl,
          max: 100, // 最大缓存项数
          isGlobal: true,
        };
      },
    }),
  ],
  providers: [CacheService],
  exports: [CacheService],
})
export class CacheModule {}
